package com.jwt.authorization.config;

import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

/**
 * @description: spring security oauth2 的授权服务器的配置
 * @Author C_Y_J
 * @create 2022/1/7 11:35
 **/
@Configuration
@AllArgsConstructor
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private final AuthenticationManager authenticationManager;

    private final TokenStore tokenStore;

    private final UserDetailsService userDetailsService;

    private final JwtAccessTokenConverter jwtAccessTokenConverter;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
        // 配置端点
        endpoints.tokenStore(tokenStore)
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService);

        // 设置access_token转换器
        endpoints.accessTokenConverter(jwtAccessTokenConverter);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                // 配置内存中
                .inMemory()
                // 该客户端id
                .withClient("client_1")
                // 该客户端密钥
                .secret(new BCryptPasswordEncoder().encode("client_1_secret"))
                // 该客户端token有效时间（秒）
                .accessTokenValiditySeconds(3600)
                // 该客户端支持的模式
                .authorizedGrantTypes("refresh_token", "password", "authorization_code", "implicit")
                // 该客户端允许的授权范围配置
                .scopes("userInfo", "server", "all")
                // 该客户端资源列表
                .resourceIds("resource")
                // false 允许跳转到授权页面
                .autoApprove(false)
                // 该客户端验证回调地址
                .redirectUris("http://www.baidu.com");

    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        // 允许表单提交
        security.allowFormAuthenticationForClients();
        // 校验token的条件是已经通过身份认证的
        security.checkTokenAccess("isAuthenticated()");
    }
}
